package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.service.ValidatecodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 统一短信发送验证码
 */
@RestController
@RequestMapping("/validatecode")
@Slf4j
public class ValidatecodeController {

    @Autowired
    private ValidatecodeService validatecodeService;

    /**
     * 体检预约发送验证码
     * @param telephone
     * @return
     */
    @PostMapping("/send4Order")
    public Result send4Order(String telephone){
        log.info("体检预约发送验证码, {}", telephone);
        try {
            validatecodeService.sendCode(telephone);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL, null);
        }
    }

    /**
     * 手机号快速登录时发送验证码
     * @return
     */
    @GetMapping("/send4Login")
    public Result send4Login(String telephone){
        log.info("手机号快速登录时发送验证码，{}", telephone);
        try {
            validatecodeService.send4Login(telephone);
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS, null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL, null);
        }
    }
}
